package com.manor.paradise.adapter

import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.manor.common.utils.DateUtil
import com.manor.common.utils.frontAndBack
import com.manor.common.utils.scaleDown
import com.manor.paradise.R
import com.manor.paradise.bean.MyHireBean
import kotlinx.android.synthetic.main.item_paradise_already_feed.view.*
import java.math.BigDecimal
import java.text.SimpleDateFormat

class AlreadyFeedAnimalAdapter(data: MutableList<MyHireBean.RecordsBean>?) : BaseQuickAdapter<MyHireBean.RecordsBean, BaseViewHolder>(R.layout.item_paradise_already_feed, data) {

    override fun convert(holder: BaseViewHolder, item: MyHireBean.RecordsBean) {

        holder.itemView.sHaveOutputTv.text = "已产 ${item.producedCrystalOre.scaleDown(4)}"
        holder.itemView.sLeftOutputTv.text = "剩余 ${item.totalCrystalOre.subtract(item.producedCrystalOre).scaleDown(4)}"
        var jzName = ""
        when(item.spiritId){
            1 ->{//乌骨鸡-》胖胖熊
                jzName = "胖胖熊 能量卷轴(赠送)"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_xiong))
            }
            2 ->{//茶花鸡-》咕咕鸡
                jzName = "咕咕鸡 勤奋卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_ji))
            }
            3 ->{//贵妃鸡-》汪汪狗
                jzName = "汪汪狗 忠诚卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_gou))
            }
            4 ->{//元宝鸡-》哼哼猪
                jzName = "哼哼猪 富贵卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_zhu))
            }
            5 ->{//五黑鸡-》咩咩羊
                jzName = "咩咩羊 吉祥卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_yang))
            }
            6 ->{//三黄鸡-》哞哞牛
                jzName = "哞哞牛 健康卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_niu))
            }
            7 ->{//芦花鸡-》乐乐虎
                jzName = "乐乐虎 成功卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_hu))
            }
            8 ->{//珍珠鸡-》淘淘凤
                jzName = "淘淘凤 进取卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_feng))
            }
            9 ->{//天山雪鸡-》米米龙
                jzName = "米米龙 尊贵卷轴"
                holder.itemView.sMyFeedAvatarIv.setImageDrawable(ContextCompat.getDrawable(holder.itemView.context, R.drawable.jz_long))
            }
        }
//        Glide.with(context).load(item.spiritImgUrl).thumbnail(0.1f).into(holder.itemView.sMyFeedAvatarIv)
        holder.itemView.sJzNameTv.text = jzName
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

        //为什么要把这种容易变动规则的计算放客户端？因为后台说后端计算会导致服务器冒烟！牛批！！！


        if (item.spiritId == 1 || item.spiritId == 2) {
            val asa: BigDecimal = item.activeDuration.subtract(item.activeDays)//可活跃时长
            val bsb: BigDecimal = BigDecimal(DateUtil.differentDays(simpleDateFormat.parse(item.currentTime), simpleDateFormat.parse(item.invalidDate.toString())).toString())
            val csc: BigDecimal = if ((asa) <= bsb) bsb else asa
            holder.itemView.sGrowthCycleTv.text = "成长周期：" + csc.toString() + "天"
        } else {
            val nnn = BigDecimal(DateUtil.differentDays(simpleDateFormat.parse(item.effectiveDate),
                    simpleDateFormat.parse(item.currentTime.toString())).toString())
            if ((nnn).compareTo(BigDecimal.ZERO) < 0) {
                holder.itemView.sGrowthCycleTv.text = "成长周期：" + item.activeDuration + "天"
            } else {
                val residue = if ( item.activeDuration.subtract(nnn).subtract(BigDecimal.ONE)  >= BigDecimal.ZERO)
                    item.activeDuration.subtract(nnn).subtract(BigDecimal.ONE)
                else
                    BigDecimal.ZERO

                holder.itemView.sGrowthCycleTv.text = "成长周期：${residue}天"
            }

        }


        val sc = item.activeDays.divide(item.activeDuration, 2, BigDecimal.ROUND_DOWN)
        val a = sc.multiply(BigDecimal("100")).intValueExact()
        holder.itemView.sGrowthTv.text = "成长值 " + "/".frontAndBack(item.activeDays, item.activeDuration)
        holder.itemView.sTaskProgressSb.isEnabled = false
        holder.itemView.sTaskProgressSb.progress = a

    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
    }

    override fun onViewAttachedToWindow(holder: BaseViewHolder) {
        super.onViewAttachedToWindow(holder)
    }


}

